#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include "sensor_drvcfg.h"
//#include "sensor_common.h"

#include "../drv/jxf22_drv_cfg.c"
#include "../drv/jxh42_drv_cfg.c"

#include "../drv/imx185_drv_cfg.c"
#include "../drv/imx290_drv_cfg.c"
#include "../drv/imx322_drv_cfg.c"
//
#include "../drv/ov2718_drv_cfg.c"
#include "../drv/ov2710_drv_cfg.c"
//
#include "../drv/bf3239_drv_cfg.c"
//
#include "../drv/n3_drv_cfg.c"
#include "../drv/pr2000_drv_cfg.c"
#include "../drv/xc9080_h65_drv_cfg.c"
#include "../drv/xc9080_hm2131_drv_cfg.c"

SGKS_VI_SensorDrvCfgT* sensor[] =
{
    &jxf22_drvcfg_info,
    &jxh42_drvcfg_info,

    &imx185_drvcfg_info,
    &imx290_drvcfg_info,
    &imx322_drvcfg_info,

    &ov2718_drvcfg_info,     
    &ov2710_drvcfg_info,     

    &bf3239_drvcfg_info,

    &n3_drvcfg_info,
    &pr2000_drvcfg_info,
    &xc9080_h65_drvcfg_info,
    &xc9080_hm2131_drvcfg_info,    
};

///////////////////////////////////////////////////////////////////////////////

int calc_line_length(SGKS_VI_SensorDrvCfgT *p_drvcfg)
{
    int i, j, k;

    SGKS_SensorHMLRegT     *p_hmax_reg;
    SGKS_SensorHMLRegT     *p_vmax_reg;
    int fmt_num;
    int h_pclk_ref;
    int v_line_ref;

    int data;

    int line_length = 0;
    int frame_length = 0;
    
    if (p_drvcfg==NULL)
    {
        printf("error\n");
        return -1;
    }

    p_hmax_reg    = p_drvcfg->ExposurePart.hmax_reg;
    p_vmax_reg    = p_drvcfg->ExposurePart.vmax_reg;

    fmt_num    = p_drvcfg->VideoFmtPart.fmt_num;     
    h_pclk_ref = p_drvcfg->ExposurePart.h_pclk_ref;
    v_line_ref = p_drvcfg->ExposurePart.v_line_ref;

    for (i=0; i<SGKS_VI_SENSOR_INTERFACE_NUM; i++)
    {
        for (j=0; j<fmt_num; j++)
        {   
            line_length = 0;
            p_hmax_reg    = p_drvcfg->ExposurePart.hmax_reg;
            while (p_hmax_reg->reg != SGKS_VI_SENSOR_TABLE_FLAG_END)
            {
                for (k=0; k<SGKS_VI_SENSOR_VIDEO_FORMAT_REG_NUM; k++)
                {
                    if (p_hmax_reg->reg==p_drvcfg->VideoFmtPart.VideoFmtInfo[i][j].fmt_reg[k].reg)
                    {
                        data = p_drvcfg->VideoFmtPart.VideoFmtInfo[i][j].fmt_reg[k].data;
                        //printf("i:%d, j:%d, p_hmax_reg->reg:0x%08x, data:0x%08x\n", i, j, p_hmax_reg->reg, data);
                        line_length += (data&p_hmax_reg->mask)<<p_hmax_reg->shift_bits;

                    }
                }

                p_hmax_reg++;
            }
            if (h_pclk_ref == SGKS_SENSOR_H_PCLK_X2)
                line_length = line_length*2;
            else if (h_pclk_ref == SGKS_SENSOR_H_PCLK_DIV2)
                line_length = line_length/2;
            else
                line_length = line_length;  
            
            p_drvcfg->VideoFmtPart.VideoFmtInfo[i][j].vi_info.line_length = line_length;
            
            //printf("i:%d, j:%d, p_hmax_reg->reg:0x%08x, line_length:0x%08x\n", i, j, p_hmax_reg->reg, line_length);
            //printf("i:%d, j:%d, line_length:%d\n", i, j, line_length);   


            frame_length = 0;
            p_vmax_reg    = p_drvcfg->ExposurePart.vmax_reg;        
            while (p_vmax_reg->reg != SGKS_VI_SENSOR_TABLE_FLAG_END)
            {
                for (k=0; k<SGKS_VI_SENSOR_VIDEO_FORMAT_REG_NUM; k++)
                {
                    if (p_vmax_reg->reg==p_drvcfg->VideoFmtPart.VideoFmtInfo[i][j].fmt_reg[k].reg)
                    {
                        data = p_drvcfg->VideoFmtPart.VideoFmtInfo[i][j].fmt_reg[k].data;
                        frame_length+= (data&p_vmax_reg->mask)<<p_vmax_reg->shift_bits;
                    }
                }
                //printf("i:%d, j:%d, frame_length:%d\n", i, j, frame_length);
                p_vmax_reg++;
            }

            if (v_line_ref == SGKS_SENSOR_V_LINE_X2)
                frame_length = frame_length*2;
            else if (v_line_ref == SGKS_SENSOR_V_LINE_DIV2)
                frame_length = frame_length/2;
            else
                frame_length = frame_length;             
            
            p_drvcfg->VideoFmtPart.VideoFmtInfo[i][j].vi_info.frame_length = frame_length;

            //printf("i:%d, j:%d, frame_length:%d, line_length:%d\n", i, j, frame_length, line_length);   
        }

     
    } 

    return 0;  
}   

int main(int argc, char* argv[])
{
    FILE *fp = NULL;
    int i;
    int size;
    int count;
    char name[64];
    size    = sizeof(SGKS_VI_SensorDrvCfgT);
    count   = sizeof(sensor)/sizeof(sensor[0]);
    for(i=0;i<count;i++)
    {
        calc_line_length(sensor[i]);
    
        sprintf(name, "./cfg/%s.drvcfg", sensor[i]->HwInfoPart.DevInfo[0].name);
        fp = fopen(name, "wb");
        if(!fp)
        {
            printf("create %s error\n", name);
            return -1;
        }
        fwrite(sensor[i], 1, size, fp);
        fclose(fp);
        printf("create %s success\n", name);
    }

    printf("\nall done\n");

    return 0;
}

